import 'package:flutter/material.dart';
import 'package:my_salary/core/app_initializer.dart';

class LocaleController extends ChangeNotifier {
  static const _localeKey = 'app_locale';
  Locale? _locale;

  Locale? get locale => _locale;

  Future<void> loadLocale() async {
    final code = AppInitializer.prefs.getString(_localeKey);
    if (code != null) {
      _locale = Locale(code);
    }
    notifyListeners();
  }

  Future<void> changeLocale(Locale? locale) async {
    _locale = locale;
    if (locale == null) {
      await AppInitializer.prefs.remove(_localeKey);
    } else {
      await AppInitializer.prefs.setString(_localeKey, locale.languageCode);
    }
    notifyListeners();
  }
}
